<?php
class ResourceProviders {
	var $htmlString;

	function getSkillMatrix($year=null,$month=null) {
		global $dbname, $db, $db_selected;
		/*
		 * Check for valid entry - we're only concerned with
		 * kids up to 36 months
		 */
		if ( ($year>3) || (($year == 3) && ($month>0)) ) {
			return false;
		} else {
			/*
			 * Get age in months
			 */
			if (!is_numeric($year)) {
				$year = 0;
			}
			
			if ($_SESSION['language'] == 'en') {
				$languageCol = 'LanguageEnglish';
			} elseif ($_SESSION['language'] == 'es') {
				$languageCol = 'LanguageSpanish';
			} elseif ($_SESSION['language'] == 'ru') {
				$languageCol = 'LanguageRussian';
			}
			
			$ageInMonths = ($year*12)+($month);
			$sql = "SELECT * from cdc_DevelopmentalChart 
					WHERE 
					AgeLower <= '$ageInMonths' AND
					AgeUpper >= '$ageInMonths' AND
					$languageCol = '1'				
					ORDER BY DisplayOrder";
			$result = mysql_db_query($dbname,$sql);
			if (mysql_num_rows($result)>0) {
				$this->htmlString = '<table class="gridTable">';
				$this->htmlString .= '<tr><th><strong>Mastered Skills</strong><br />(most kids can do)</th>';
				$this->htmlString .= '<th><strong>Emerging Skills</strong><br />(half of kids can do)</th>';
				$this->htmlString .= '<th><strong>Advanced Skills</strong><br />(a few kids can do)</th></tr>';
				$a=0;
				while ($value = mysql_fetch_array($result)) {
					if (($a%2)>0) {
						$rowClass = 'altRow';
					} else {
						$rowClass = '';
					}
					
					/*
					 * Parse skills coding
					 */
					$codeArray = explode('|',$value['SkillsCoding']);
					
					$this->htmlString .= '<tr class="'.$rowClass.'">';
					if (!empty($value['SkillsMastered'])) {
						$this->htmlString .= '<td><input type="checkbox" name="Mastered_'.$a.'" value="'.$codeArray[0].'" /><input type="hidden" name="HiddenMastered_'.$a.'" value="'.$codeArray[0].'" /> '.$value['SkillsMastered'].'</td>';
					} else {
						$this->htmlString .= '<td></td>';
					}
					if (!empty($value['SkillsEmerging'])) {
						$this->htmlString .= '<td><input type="checkbox" name="Emerging_'.$a.'" value="'.$codeArray[1].'" /><input type="hidden" name="HiddenEmerging_'.$a.'" value="'.$codeArray[1].'" /> '.$value['SkillsEmerging'].'</td>';
					} else {
						$this->htmlString .= '<td></td>';
					}
					if (!empty($value['SkillsAdvanced'])) {
						$this->htmlString .= '<td><input type="checkbox" name="Advanced_'.$a.'" value="'.$codeArray[2].'" /><input type="hidden" name="HiddenAdvanced_'.$a.'" value="'.$codeArray[2].'" /> '.$value['SkillsAdvanced'].'</td>';
					} else {
						$this->htmlString .= '<td></td>';
					}
						
					$this->htmlString .= '</tr>';
					$a++;
				}
				$a=$a-1;
				$this->htmlString .= '</table>';
				$this->htmlString .= '<input type="hidden" name="matrixRows" value="'.$a.'" />';
			} else {
				$this->htmlString .= 'Your resource provider search did not find any library items. Please try another search.';	
				return $this->htmlString;
			}	
		}
	}
	
	function getResourceProviders() {
		global $dbname, $db, $db_selected;
		
		/*
		 * Find out which milestones are not checked.
		 */
		$b=0;
		$notCheckedArray = array();
		for($a=0;$a<=($_POST['matrixRows']);$a++) {
			/*
			 * Let's just get stuff from the Mastered and Emerging columns.
			 */
			if (empty($_POST['Mastered_'.$a])) {
				$notCheckedArray[$b] = $_POST['HiddenMastered_'.$a];
				$b++;
			}
			if (empty($_POST['Emerging_'.$a])) {
				$notCheckedArray[$b] = $_POST['HiddenEmerging_'.$a];
				$b++;
			}
			/*
			if (empty($_POST['Advanced_'.$a])) {
				$notCheckedArray[$b] = 'Advanced_'.$_POST['Advanced_'.$a];
				$b++;
			}
			*/
		}
		/*
		 * Get only unique array items
		 */
		$notCheckedArrayUnique = array_unique($notCheckedArray);
		
		/*
		 * Create SQL query
		 */
		$sql = "SELECT * from cdc_ResourceProviders 
				WHERE
				ProviderZip = '".$_POST['zip']."' AND (";
		$codeString = '';
		foreach($notCheckedArrayUnique as $code) {
			$sql .= " ProviderCoding = '".$code."' OR";
			$codeString .= $code;
		}
		$sql .= " ProviderCoding = '".$codeString."'";
		$sql .= ")";
		$result = mysql_db_query($dbname,$sql);
		$j=0;
		while ($value = mysql_fetch_array($result)) {
			if (($j%2)>0) {
				$rowClass = 'altRow';
			} else {
				$rowClass = '';
			}
			$this->htmlString .= $this->renderResourceListItem($value,$rowClass);
			$j++;
		}
		$this->htmlString .= '<div class="blueRow" style="padding: 8px;"><p class="helpContainer">';
		$this->htmlString .= '<b style="font-size: 1.5em;">Still need help?</b> Let us know what you are looking for and we will contact you as soon as possible.<br /><br />';
		
		$contact = new Contact();
		$contactForm = $contact->createContactForm();
		$this->htmlString .= $contact->renderContactHTML();
		
		$this->htmlString .= '</p></div>';
	}
	
	function renderResourceListItem($itemArray,$rowClass) {
		$itemID      = $itemArray['ProviderID'];
		$itemName    = $itemArray['ProviderName'];
		$itemDesc    = stripslashes($itemArray['ProviderDescription']);
		$itemURL     = $itemArray['ProviderWebURL'];
		$itemEmail   = $itemArray['ProviderEmail'];
		$itemPhone   = $itemArray['ProviderPhone'];
		$itemAddress = $itemArray['ProviderAddress'];
		$itemCity    = $itemArray['ProviderCity'];
		$itemState   = $itemArray['ProviderState'];
		$itemZip     = $itemArray['ProviderZip'];
		
		if (!empty($itemAbstract)) {
			$itemAbstract = shortText($itemAbstract,30);
		}
		$itemString  = '<div class="'.$rowClass.'" style="padding: 8px;"><p><a href="resourceView.php?item='.$itemID.'" class="bigLink">'.$itemName.'</a></p>';
		$itemString .= '<p style="margin: 0 0 10px 15px;">'.$itemDesc.'';
		$itemString .= '<div style="width: 45%; float: left;">';
		
		if (!empty($itemPhone)) {
			$itemString .= '<p class="iconPhone">'.$itemPhone.'</p>';
		}
		if (!empty($itemURL)) {
			$itemString .= '<p><a href="'.$itemURL.'" class="iconWebsite">'.$itemURL.'</a></p>';
		}
		if (!empty($itemEmail)) {
			$itemString .= '<p><a href="mailto:'.$itemEmail.'" class="iconEmail">'.$itemEmail.'</a></p>';
		}
		
		$itemString .= '</div>';
		$itemString .= '<div style="width: 45%; float: left;"></div>';
		$itemString .= '<div style="clear: left; height: 10px;"></div>';
		$itemString .= '</p></div>';
		
		return $itemString;
	}
	
	function renderResourceSearchHTML() {
		return $this->htmlString;
	}
}	
?>